Skip to content

Encode error objects into json before logging them - #668

Open
jadhavgaurav wants to merge 2 commits into
tarantool:masterfrom
jadhavgaurav:gh-651-json-encode-errors
Open

jadhavgaurav wants to merge 2 commits into
tarantool:masterfrom
jadhavgaurav:gh-651-json-encode-errors

Conversation

@jadhavgaurav

Copy link
Copy Markdown

Closes #651. Also closes #624, which reports the same symptom for the replication_status ... is unknown message.

Problem

VShard error objects are plain Lua tables carrying __tostring = json.encode (vshard/error.lua). Tarantool's log module formats its arguments through string.format(), and only recent LuaJIT versions call __tostring for %s. On the older Tarantool versions vshard still supports, an error object is printed as a bare table address:

E> Error during searching in cluster for recovery of 9 bucket: table: 0x402b2f78

The recovery service was already fixed in c347440 ("Let's fix only recovery for now"). This does the rest.

Change

json_encode(err) at every remaining log/set_status_error site that prints an error object:

  • vshard/storage/init.lua: recovery, garbage collector, master_sync and rebalancer services, plus the rebalancer worker and route-applying messages.
  • vshard/router/init.lua: both discovery failure messages.
  • vshard/replicaset.lua: initial-handshake close, replica_call exception, replica backoff, master locate/search, failover ping and step, worker service failure, and the replication_status update from replica_failover_ping (that last one is Malformed replication status log message #624).
  • vshard/util.lua: the reloadable fiber loop failure message.

Two notes on scope:

  • Where the value comes from pcall() and the surrounding code already normalised it with lerror.make(), the lerror.make() call is kept and wrapped. In replica_call() the existing lerror.make(err) is hoisted above the log so the logged object and the returned object are the same one.
  • Messages built from pcall() over plain box API calls (dropping on_replace triggers, _schema upgrade/downgrade in vshard/storage/schema.lua) are deliberately left alone: those errors are box.error cdata, which never prints as a table address.

json.encode is safe for every shape that reaches these sites: an error table, a box.error cdata (serialised to its message) and a plain string all encode without raising.

Testing

No new test, matching NO_TEST=minor on c347440. The bug is invisible on any Tarantool where string.format('%s', obj) honours __tostring, which is the case for every version I can run locally — the pre-fix code already emits the same json there. test/storage-luatest/storage_1_1_1_test.lua already asserts json content ('Error during downloading rebalancer states:.*"replicaset_id":"%s"') for one of these messages; this change makes that hold on the old versions too.

Full suite run on Tarantool 3.8.1 (macOS): 93 passed, 1 skipped, 5 disabled, 1 failed. The single failure is upgrade/upgrade.test.lua, which fails identically on unmodified master here with a test-run harness error (RuntimeError: dictionary changed size during iteration in preprocessor.py), so it is unrelated to this change. I could not test against an old Tarantool where the bug actually reproduces, so the behaviour on those versions is reasoned about rather than observed.

@Serpentian
Serpentian self-requested a review September 7, 2026 15:19
@Serpentian Serpentian self-assigned this Sep 7, 2026

@Serpentian Serpentian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulations on your first contribution! Great patch, let's work a little bit more with it)

Comment thread vshard/router/init.lua Resolved
Comment thread vshard/storage/init.lua Resolved
Comment thread vshard/router/init.lua Resolved
Comment thread vshard/replicaset.lua Resolved
@jadhavgaurav
jadhavgaurav force-pushed the gh-651-json-encode-errors branch from d82be73 to a239eed Compare September 29, 2026 10:45
@jadhavgaurav

Copy link
Copy Markdown
Author

Thanks for the review. I amended the commit and force-pushed (a239eed). Per point:

  1. Test. Added replicaset.test_replica_call_logs_errors_as_json in test/replicaset-luatest/replicaset_3_test.lua. It makes replica_call() fail with a string, a box.error and a vshard error and checks that each is logged as json, so box.error is now covered too. NO_TEST=minor is gone. I also added unit tests for the __tostring change in test/unit-luatest/error_test.lua. On Tarantool 3.8.1 the new tests fail against the current master sources (the string error is logged as a bare message) and pass with the patch. I only have 3.8.1 locally, so the old-Tarantool cases rely on CI.

  2. tostring. Every error log site now uses tostring(lerror.make(err)) instead of json_encode(err), so strings and box.errors are logged as json through the same path. json_encode is kept only for non-error values (bucket lists, routes). I also converted the two recovery sites fixed in c347440, for consistency.

  3. Commit message is now log: encode error objects into json before logging them.

  4. Safe __tostring. The three metatables in vshard/error.lua now share one __tostring. It tries json.encode under pcall. If that raises, it encodes a copy of the error that keeps only strings, numbers, booleans and tables with string keys (so prev chains survive). If that still raises (for example json.cfg{encode_invalid_numbers = false} with a NaN), it returns unencodable error: <message>. Both of your repros now print: the [false] payload one logs the error with an empty payload, the function one logs {"message":"original error"}. The replica_call test covers an error with a function field, and the unit tests cover the [false] key, the nested prev and the last-resort path.

Full test-run suite passes locally except upgrade/upgrade.test.lua, which fails the same way on master here (a test-run harness error, dictionary changed size during iteration). failover and multiple_routers each failed once in a parallel run because of local resource issues (ENOBUFS and a leftover instance holding a port) and pass when re-run alone.

@Serpentian Serpentian assigned Serpentian and unassigned jadhavgaurav Oct 1, 2026

@Serpentian Serpentian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're moving fast! Great!

Comment thread vshard/error.lua Resolved
Comment thread vshard/error.lua Resolved
Comment thread vshard/error.lua Resolved
Comment thread vshard/router/init.lua Resolved
Comment thread vshard/storage/init.lua Resolved
@Serpentian Serpentian assigned jadhavgaurav and unassigned Serpentian Oct 2, 2026
@jadhavgaurav
jadhavgaurav force-pushed the gh-651-json-encode-errors branch from a239eed to 8f37849 Compare October 8, 2026 08:22
@jadhavgaurav

Copy link
Copy Markdown
Author

Thanks, all points addressed. Force-pushed two commits (8f37849 on top of 991623d), per point:

  1. Tests: the new tests are at the end of their files (replicaset_3_test.lua, error_test.lua).
  2. Split: error: make the error stringification safe has the __tostring fix and its unit tests (they fail without it). error: log errors as json on old Tarantool has the logging changes and test_replica_call_logs_errors_as_json (string, box.error and vshard error; fails without the source change).
  3. Fallback: if json.encode raises, __tostring returns tostring(err.message). The multi-level fallback and the unencodable case of the replica_call test are dropped.
  4. Comments: removed the obvious ones, kept a short one on why __tostring must not throw.
  5. Commit prefix is error: .
  6. All rows of your table now log tostring(lerror.make(err)): _bucket, _truncate, _schema (both) trigger removal, config rollback (guard_err), schema upgrade, downgrade and version update failures, and the reload evolution migration failure. The quotes around %s in the schema messages are gone, since json is self-delimited.

Local run on Tarantool 3.8.1: luatest and test-run suites pass, luacheck has only the two existing _opts warnings. upgrade/upgrade.test.lua fails on the test-run harness the same way as on master. failover failed once in the parallel run and passes alone. I can only test on 3.8.1, so the old-Tarantool behaviour relies on CI.

@jadhavgaurav
jadhavgaurav force-pushed the gh-651-json-encode-errors branch from 8f37849 to bd328e2 Compare October 19, 2026 11:46
@jadhavgaurav

Copy link
Copy Markdown
Author

Rebased onto master (conflicts in vshard/storage/init.lua and vshard/storage/schema.lua, where master dropped the pcall calls my logging change touched), no functional change; ready for another look. New head: bd328e2

@Serpentian
Serpentian self-requested a review October 19, 2026 12:25

@Serpentian Serpentian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though we dropped the support for Tarantool 1.10 (where the problem with crutchy table logging was present), I think the patch is still useful to make error logging more safe and consistent. These look like the last comments from me! Thank you for fixing all the nits!

VShard errors are stringified with json.encode() as __tostring. It
raises on the values it can't encode: a function or a table key, which
is neither a number nor a string. An error can carry such values, for
example in a payload of a custom box.error. Errors are stringified when
they are logged, so the failed encoding breaks the logging itself and
hides the original problem.

Return the error message if the error can't be encoded. Also use one
metatable for all the error kinds instead of the three equal ones.

Part of tarantool#651

NO_DOC=bugfix

Signed-off-by: Gaurav Jadhav <gaurav.vjadhav01@gmail.com>
@jadhavgaurav
jadhavgaurav force-pushed the gh-651-json-encode-errors branch from bd328e2 to ca1a619 Compare November 11, 2026 05:46
@jadhavgaurav

Copy link
Copy Markdown
Author

Done. The test now runs on replica_1_a instead of starting a new node, and an after_test hook closes the extra connection and restores the ratelimit setting and the globals I added. I also shortened the commit message of the second commit and removed the claim about old Tarantool versions. Should I also drop "on old Tarantool" from its subject?

@Serpentian
Serpentian self-requested a review November 11, 2026 08:32

@Serpentian Serpentian left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, a little bit more comments) Thank you for working on that!

Comment thread vshard/storage/init.lua Resolved
@jadhavgaurav
jadhavgaurav force-pushed the gh-651-json-encode-errors branch from ca1a619 to 2c51863 Compare November 13, 2026 06:21
@jadhavgaurav

Copy link
Copy Markdown
Author

Thanks, both done.

The cleanup is now plain statements at the end of test_replica_call_logs_errors_as_json, and the after_test hook is gone.

On moving tostring into the ratelimiter: good idea, done. log_info/log_warn/log_error now call tostring on table arguments (numbers and strings are left alone, so %d formats still work), and I removed the manual tostring from the places that pass the error to the limiter directly, including the two recovery ones. The places that build the message with set_status_error still need it, so the patch is not shorter overall, but a new limiter:log_*(..., err) call cannot miss it any more. I also added tostring at the missed place in the master synchronization service.

One caveat: on Tarantool 3.8.1 the new test also passes with only the ratelimiter change reverted, because this version already honours __tostring for table arguments. So I could not show the limiter change with a test here. The rest of the patch fails the new test when reverted.

@Serpentian
Serpentian self-requested a review November 18, 2026 08:33

@Serpentian Serpentian left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last comment, see in the thread above

VShard error objects are plain Lua tables with a __tostring metamethod.
Several log messages print them with the '%s' format, which does not
always call __tostring, so the error is not logged readably.

The recovery service was already fixed in c347440. Do the same for the
rest of the log messages, which print an error: call tostring() on the
error passed through lerror.make(). lerror.make() also converts strings
and box errors, so they are logged as json too instead of just the
message.

Closes tarantool#651
Closes tarantool#624

NO_DOC=bugfix

Signed-off-by: Gaurav Jadhav <gaurav.vjadhav01@gmail.com>
@jadhavgaurav
jadhavgaurav force-pushed the gh-651-json-encode-errors branch from 2c51863 to bf9f98b Compare November 23, 2026 04:42
@Serpentian
Serpentian self-requested a review November 23, 2026 10:22

@Serpentian Serpentian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but I think that adding the tostring is the wrong road here. We started the review process, when Tarantool 1.10 was still supported and the issue really existed. Now we support only Tarantool >= 2.11, which properly handles the tostring and it became excessive. I propose to have the following patchset:

  • error: make the error stringification safe -- as it's now
  • Revert "storage: fix error logging in the recovery service"
  • error: convert all errors for consistent logging -- lerror.make

Comment thread vshard/router/init.lua
limiter:log_warn(err, service:set_status_error(
'Error during discovery %s, retry will be done later: %s',
rs_id, err))
rs_id, tostring(lerror.make(err))))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tarantool 1.10 is not supported by VShard anymore, and the minimum supported version (2.11) already has no problems with calling the tostring by itself. So this new tostring is excessive. Let's drop it from the patch.

The lerror.make can remain. The motivation is consistent logging of errors: otherwise Tarantool's error's are logged as message only, but vshard ones: with code, fields, message and so on. The commit msg should be updated.

Comment thread vshard/storage/init.lua
log.info(start_format, type)
limiter:log_error(err,
'Error during recovery of bucket %s on replicaset %s: %s',
bucket_id, peer_id, json_encode(err))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This json_encode is excessive too now, let's make the commit, which reverts the c347440 and describe the reason for that revert in the commit msg: the minimum supported version already properly calls the tostring of the tables. This revert commit can also close the issues (since old tarantool, which caused them are not supported anymore) and be marked as part of #673

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Printing errors should be wrapped with json_encode function Malformed replication status log message

2 participants